from bs4 import BeautifulSoup

with open('../素材/豆瓣.html', 'r', encoding='utf-8') as f:
    html = f.read()

soup = BeautifulSoup(html, 'lxml')

# .语法  soup对象.属性（标签名）
# 获取页面标题标签
# print(soup.title)
# 获取页面中的div标签  是获取的第一个 也只获取一个
# print(soup.div)
# print(soup.divx)  # None
# print(soup.a)
# print(soup.img)

# find 方法  上面的.语法 是它的简写
# print(soup.title)
# print(soup.find('title'))
# print(soup.div)
# print(soup.find('div'))
# print(soup.divx)  # None
# print(soup.find('divx'))  # None

# 获取属性
# print(soup.div)
# print(soup.div['class'])  # 获取class属性
# print(soup.div['id'])  # 获取id属性
# print(soup.div.attrs)
# print(soup.div.attrs['id'])
# print(soup.div.attrs['class'])

# print(soup.find('div'))
# print(soup.find('div')['class'])
# print(soup.find('div')['id'])
# print(soup.find('div').attrs)
# print(soup.find('div').attrs['id'])
# print(soup.find('div').attrs['class'])

# 添加条件进行查找
# print(soup.find('div', id="db-nav-book"))
# print(soup.find('div', class_="nav"))
# print(soup.find('div', class_="grid-12-12 clearfix"))
# print(soup.find('div', id="db-nav-book", class_="nav"))

# print(soup.find('div', attrs={'id': "db-nav-book"}))
# print(soup.find('div', attrs={'class': "nav"}))
# print(soup.find('div', attrs={'class': "grid-12-12 clearfix"}))
# print(soup.find('div', attrs={'id': "db-nav-book", 'class': "nav"}))

# .和find的组合使用
# print(soup.find('div', class_="nav-logo"))
# print(soup.find('div', class_="nav-logo").a)
# print(soup.find('div', class_="nav-logo").find('a'))

# print(soup.find('div', id="db-nav-book").find('div', class_="nav-logo"))
# print(soup.find('div', id="db-nav-book").find('div', class_="nav-logo").a)
# print(soup.find('div', id="db-nav-book").find('div', class_="nav-logo").a.attrs['href'])
# print(soup.find('div', id="db-nav-book").find('div', class_="nav-logo").a['href'])
# print(soup.find('div', id="db-nav-book").find('div', class_="nav-logo").find('a')['href'])

# print(type(soup.find('div', id="db-nav-book").find('div', class_="nav-logo").find('a')['href']))
# print(type(soup.find('div', id="db-nav-book").find('div', class_="nav-logo").find('a')))

# 持久化存储需要注意的点
with open('con.txt', 'w', encoding='UTF-8') as f:
    # f.write(soup.find('div', id="db-nav-book").find('div', class_="nav-logo").find('a'))
    f.write(str(soup.find('div', id="db-nav-book").find('div', class_="nav-logo").find('a')))



# 获取文本内容
# print(soup.title)
# print(soup.title.string)
# print(soup.title.text)
# print(soup.title.get_text())
# print(soup.title.strings)
# print(list(soup.title.strings))
# print(soup.title.stripped_strings)
# print(list(soup.title.stripped_strings)) #    去除空白符

# 区别 多层嵌套
# print(soup.find('div'))
# print(soup.find('div').string)  # None
# print(soup.find('div').text)  # 获取所有文本
# print(soup.find('div').get_text())  # 获取所有文本
# print(list(soup.div.strings)) # 获取所有文本
# print(list(soup.div.stripped_strings)) # 获取所有文本  去除空白符


import re
# print(re.sub('\s', '',''.join(list(soup.div.strings)))) # 获取所有文本

# 美化打印
# print(soup.find('div').prettify())

# 获取所有 find_all  和find使用方式一样
# print(soup.find_all('a'))
# print(soup.find_all('img'))
# print(soup.find_all('span'))
# print(soup.find_all('p', class_="color-gray"))
# print(soup.find_all('p', class_="color-gray"))
# print(soup.find_all('p', attrs={'class':"color-gray"}))

# print(soup.find_all('ul',class_="cover-col-4 clearfix")[0])
# print(type(soup.find_all('ul',class_="cover-col-4 clearfix")[0]))
# print(soup.find_all('ul',class_="cover-col-4 clearfix")[0].find_all('li'))
# print(soup.find('ul',class_="cover-col-4 clearfix").find_all('li'))

# 一般用不到
# print(soup.find('ul',class_="cover-col-4 clearfix").find_all('li', limit=2))
# print(soup.find_all(['a', 'img']))

"""
cover = soup.find_all('a',class_="cover")
# print(cover)
for a in cover:
    # print(type(a))
    # print(a.find('img'))
    # print(a.img)
    print(a.img['src'])
"""

# 选择器(用的少)  获取所有
# print(soup.select('img'))   # HTML选择器 查找所有img标签
# print(soup.select('.cover'))  # class选择器 选择所有class属性值为cover的标签
# print(soup.select('#db-nav-book')) # id 选择所有id属性值为db-nav-book的标签
# print(soup.select('.cover-col-4.clearfix')) # id 选择所有id属性值为db-nav-book的标签
# print(soup.find('div', attrs={'id': "db-nav-book", 'class': "nav"}))
# print(soup.select('.nav#db-nav-book')) # id 选择所有id属性值为db-nav-book的标签
# print(soup.select('ul'))
# print(soup.select('ul li'))
# print(soup.select('ul>li'))
# print(soup.select('ul[class="cover-col-4 clearfix"]>li'))
